﻿@using ZBJF.Warranty.WebProvider
@{
    ViewBag.Title = "销售商列表";
}
@section Header{
    @Scripts.Render("~/bundles/datatable")
    @Styles.Render("~/Content/datatable")
}
@model ZBJF.Warranty.Model.Sellers
<ul class="nav nav-tabs">
    <li class="active"><a href="/Sellers/">销售商列表</a></li>
    <li class="Add-R"><a  href="/Sellers/Edit">新建销售商</a></li>
</ul>
    <div class="breadcrumb form-search">
        <label>销售商类型</label>
        @Html.DropDownList("SellersTypeID", ConvertToSelector.Instance.SellersTypeToSelectList())
        <label>一级机构</label>
        @Html.DropDownList("PareSellersOrganizationId", ConvertToSelector.Instance.PareSellersOrganizationToSelectList(), new { @onchange = "SelectSellersOrganization()" })
        <label>二级机构</label>
        @Html.DropDownList("SellersOrganizationId", ConvertToSelector.Instance.SellersOrganizationToSelectList())
        <label>销售商名称</label>
        @Html.TextBoxFor(m => m.SellersName, new { maxlength = 20, @class = "input-small" })
        <label style="padding-top: 8px; ">
            <input id="btnSubmit" class="btn btn-primary Search-R" type="button" value="查询" onclick="search();">
            <input id="btnExport" class="btn btn-success Search-R margin-l-10" type="button" value="导出销售商清单" onclick="ExportData()">
        </label>
    </div>
<div class="table-responsive dataTables_wrapper">
  @Html.Hidden("SellerID")
    <table id="sellersList" class="table table-striped table-bordered table-hover">
        <thead>
        <tr>
            <th width="20">序号</th>
            <th width="100">销售商类型</th>
            <th width="80">销售商名称</th>
            <th width="100">销售商系统类型</th>
            <th width="80">机构详情</th>
            <th width="80">销售机构代码</th>
            <th width="80">商户员工数</th>
            <th width="80">创建时间</th>
            <th width="80">是否启用</th>
            <th width="100">操作</th>
        </tr>
        </thead>
    </table>
</div>
<div id="shows">
    <form id="FormRest">
        <table style="text-align: right; margin: 0 auto;">
            <tr>
                <th>销售商类型：</th>
                <td>@Html.DropDownList("SellerTypeName", ConvertToSelector.Instance.SellersTypeToSelectList(), new{ @style = "width:148px" })</td>
                <th>销售商名称：</th>
                <td>@Html.TextBox("SellerName", "", new { @style = "width:152px" })</td>
            </tr>
            <tr>
                <th>所属机构：</th>
                <td>
                    @Html.TextBox("Sellers_" + 1 + "__OrganizationName", "", new { validate = "{required:true,messages:{required:'请选择所属机构'}}", @readonly = "readonly", @class = "select-icon", onclick = "showSellersOrganizationLayer('_" + 1 + "__')", @style = "width:136px" })
                    @Html.HiddenFor(m => m.SellersOrganizationId)
                </td>
                <th>销售商系统类型：</th>
                <td>@Html.DropDownListFor(m => m.SaleSystemType, ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ZBJF.Warranty.Common.Enums.Base.SaleSystemTypeEnums)), new { validate = "{required:true,messages:{required:'请选择销售商系统类型！'}}" })</td>
            </tr>
            <tr>
                <th>销售机构代码：</th>
                <td>@Html.TextBoxFor(m => m.SellerCode, new { maxlength = 15, @style = "width:136px;" })</td>
                <th></th>
                <td></td>
            </tr>
        </table>
    </form>
</div>
@section scripts{
    @Scripts.Render("~/Bundles/ImagePreview")
    <script type="text/javascript">
        var indexs;
        window.onload = function () {
            document.getElementById("SellerTypeName").style.color = "#555555";
            document.getElementById("SaleSystemType").style.color = "#555555";
            $("#shows").hide();
        }
        var isSearch = false;
        $(function() {
            var objTable = $("#sellersList")
                .dataTable({
                    fnDrawCallback: function() {
                        ControlPermissionOperate();
                    },
                    "sAjaxSource": "/Sellers",
                    aoColumns: [
                        {
                            "mData": null,
                            "bSortable": false,
                            "fnRender": function(nRow) {
                                return nRow.iDataRow + 1;
                            }
                        },
                        { "mData": "SellerTypeName" },
                        { "mData": "SellersName" },
                        { "mData": "SaleSystemTypeName" },                        
                        {
                            "mData": null,
                            "fnRender": function (nRow) {
                                return "<a onclick='OpenSellersOrganizationLayer(" + nRow.aData.SellersOrganizationId + ")'>详情<a>";
                            }
                        },
                        { "mData": "SellerCode" },
                        { "mData": "EmployeeCount" },
                        { "mData": "CreateTime" },
                        { "mData": "ID" },
                        { "mData": "ID" }
                    ],
                    "fnRowCallback": function(nRow, aData, iDisplayIndex) {
                        $('td:eq(8)', nRow).html(aData.IsAvailable ? bootStrapOkIncon : bootStrapRemoveIncon);
                        var html = "<a class='Edit-R' target='_blank' onclick=\"Show(this," + aData.SellersID + ",'" + aData.OrganizationNameDesc+ "')\" title='编辑'><i class='icon-edit'></i></a>";
                        html += "<a class='Edit-R' href=\"javascript:UpdateStatus('" + aData.SellersID + "'," + !aData.IsAvailable + ",'/Sellers/UpdateSellerStatus','sellersList')\" title='" + (aData.IsAvailable ? "禁用" : "启用") + "'>" + (aData.IsAvailable ? bootStrapRemoveIncon : bootStrapOkIncon) + "</a>";
                        html += "<a class='Edit-R' target='_blank' href='/Sellers/Detail/" + aData.SellersID + "' title='产品信息配置'>信息配置</a>";
                        $('td:eq(9)', nRow).html(html);
                    }
                });
            imgPreview();
        });

        function imgPreview() {
            setTimeout(function() { $("img.preview").preview(); }, 500);
        }

        function SelectSellersOrganization() {
            var pareSellersOrganizationid = $("#PareSellersOrganizationId option:selected").val() * 1;
            $.ajax({
                url: "/Sellers/SelectSellersOrganization?pareSellersOrganizationid=" + pareSellersOrganizationid,
                type: "post",
                datatype: "json",
                success: function(data) {
                    if (data.success) {
                        $("#SellersOrganizationId").empty();
                        $("#SellersOrganizationId").append("<option value=''>请选择</option>");
                        for (var i = 0; i < data.aaData.length; i++) {
                            $("#SellersOrganizationId").append("<option value='" + data.aaData[i].ID + "'>" + data.aaData[i].OrganizationName + "</option>");
                        }
                    }
                }
            });
        }
        function OpenSellersOrganizationLayer(id) {
            layer.open({
                type: 2,
                title: '所属机构详情',
                shadeClose: true,
                shade: 0.8,
                area: ['320px', '60%'],
                content: '/PubModelDialog/SellersOrganizationSelector?chkStyle=radio&ParentCategoryID=' + id+'&Type=1' //单选
            });
        }
        function Show(obj, sellerId, organizationName) {
            document.getElementById("FormRest").reset();
            var id = $(obj).parents("td").siblings("td");
            EmptySelect("SaleSystemType");
            EmptySelect("SellerTypeName");
            $("#SellerID").val(sellerId);
            indexs = layer.open({
                type: 1,
                title: "修改详情" //标题栏
                ,
                closeBtn: false,
                area: '800px;',
                shade: 0.8,
                id: 'LAY_layuipro' //设定一个id，防止重复弹出
                ,
                resize: false,
                btn: ['确认', '取消'],
                yes: function(index, layero) {
                    UpdateSellers();
                },
                btnAlign: 'c',
                moveType: 1, //拖拽模式，0或者1
                content: $('#shows').show(),
                success: function(layero, index) {
                }
            });
            $("#SellerTypeName option:contains(" + id.eq(1).text().trim() + ")").attr("selected", true);
            $("#SellerName").val(id.eq(2).html());
            $("#Sellers_" + 1 + "__OrganizationName").val(organizationName);
            var a = "请选择";
            if (id.eq(3).text()) {
                a= id.eq(3).text().trim();
            }
            $("#SaleSystemType option:contains(" + a + ")").attr("selected", true);
            $("#SellerCode").val(id.eq(5).html());
        }

        function EmptySelect(obj) {
            var id = document.getElementById(obj);
            for (var i = 0; i < id.options.length; i++) {
                id.options[i].removeAttribute("selected");
            }
        }
        function UpdateSellers() {
            var iD = $("#SellerID").val();
            if (iD > 0) {
                var sellerTypeId = $("#SellerTypeName option:selected").val();
                var sellerName = $("#SellerName").val();
                var sellersOrganizationId = $("#SellersOrganizationId").val().trim() * 1;
                var saleSystemType = $("#SaleSystemType option:selected").val();
                var sellerCode = $("#SellerCode").val();
                $.ajax({
                    url: "/Sellers/UpdateSellers?ID=" + iD,
                    type: "post",
                    data: {
                        "SellersType": sellerTypeId,
                        "SellersName": sellerName,
                        "SellersOrganizationId": sellersOrganizationId,
                        "SaleSystemType": saleSystemType,
                        "SellerCode": sellerCode
                    },
                    dataType: "json",
                    success: function(data) {
                        if (data.success) {
                            layer.alert(data.message);
                            location.href = '/Sellers';
                        } else {
                            layer.alert(data.message);
                        }
                    }
                });
            }
        }

        function showSellersOrganizationLayer(i) {
            layer.open({
                type: 2,
                title: '请选所属机构',
                shadeClose: true,
                shade: 0.8,
                area: ['320px', '60%'],
                content: '/PubModelDialog/SellersOrganizationSelector?chkStyle=radio&elementIndex=' + i //单选
            });
        }

        //选中客户后设置值,清空选中的配件
        function setSelectedSellerOrganization(item, i) {
            if (!item.ID) //双击 单个 否则是点击确定多个
                item = item[0];
            if (!item || item == "") {
                $("#SellersOrganizationId").val("");
                $("#Sellers" + i + "OrganizationName").val("");
            } else {
                $("#SellersOrganizationId").val(item.ID);
                $("#Sellers" + i + "OrganizationName").val(item.name);
            }

        }

        function search() {
            /*给查询对象赋值*/
            queryParamList.SellersTypeID = $("#SellersTypeID").val();
            queryParamList.PareSellersOrganizationId = $("#PareSellersOrganizationId").val();
            queryParamList.SellersOrganizationId = $("#SellersOrganizationId").val();
            queryParamList.SellersName = $("#SellersName").val();
            $("#sellersList").dataTable().fnDraw();
            imgPreview();
        }

        function ExportData() {
            /*导出数据*/
            if (!confirm("确定要导出Excel吗？")) return;
            search();
            window.location.href = '@Url.Action("ExportSellersDetails")?content=' + JSON.stringify(queryParamList);
        }
    </script>
}